#ifdef USE_WAMP
#include "plc_wamp.h"
#include <AutobahnWS.h>
WebSocketsClient client1;
AutobahnWS ws;

int wamp::loop()
{
    ws.loop();
    return 0;
}
wamp_param *NewPLC(wamp_param *arg)
{
    core_debug("\nNewPLC  %s\n", arg->asString());
    arg->reset();
    arg->add("date");
    arg->add("1");

    return arg;
}
wamp_param *SetTraceVariablesList(wamp_param *arg)
{
    core_debug("\nSetTraceVariablesList  %s\n", arg->asString());
    StaticJsonBuffer<200> jsonBuffer;
    JsonArray &array2 = jsonBuffer.parseArray(arg->asString());
    String s;
    array2.printTo(s);
    core_debug(s.c_str());
    unsigned int idx = array2[0];
    int force = array2[2];
    // RegisterDebugVariable(idx, (void *)force);
    arg->reset();
    arg->add("debug");
    return arg;
}

wamp_param *GetTraceVariables(wamp_param *arg)
{
    core_debug("GetTraceVariables  %s\n", arg->asString());
    String p;
    unsigned long tick = 0;
    unsigned long size = 0;
    char *buffer = 0;
    // if (GetDebugData(&tick, &size, (void **)&buffer) == 0)
    // {

    //     StaticJsonBuffer<500> jsonBuffer;
    //     JsonObject &root = jsonBuffer.createObject();
    //     root["status"] = plc_str[plc_status];
    //     root["tick"] = tick;
    //     int encodedLength = Base64.encodedLength(size);
    //     char encodedString[encodedLength];
    //     Base64.encode(encodedString, (char *)buffer, size);
    //     root["Traces"] = encodedString;
    //     root.printTo(p);
    //     core_debug(p.c_str());
    // }
    arg->reset();
    arg->add(p.c_str());
    return arg;
}
wamp_param *PurgeBlobs(wamp_param *arg)
{
    core_debug("PurgeBlobs  %s\n", arg->asString());
    arg->reset();
    return arg;
}
wamp_param *SeedBlob(wamp_param *arg)
{
    core_debug("SeedBlob  %s\n", arg->asString());
    arg->reset();
    arg->add("debug");
    return arg;
}
wamp_param *StartPLC(wamp_param *arg)
{
    core_debug("StartPLC  %s\n", arg->asString());
    // plc_status = Started;
    arg->reset();
    arg->add(1);
    return arg;
}
wamp_param *StopPLC(wamp_param *arg)
{
    core_debug("StopPLC  %s\n", arg->asString());
    // plc_status = Stopped;
    arg->reset();
    arg->add(1);
    return arg;
}
wamp_param *GetPLCstatus(wamp_param *arg)
{
    core_debug("\nGetPLCstatus: %s\n", arg->asString());
    arg->reset();
    // arg->add(plc_str[plc_status]);
    arg->add(0);
    return arg;
}
wamp_param *MatchMD5(wamp_param *arg)
{
    core_debug("\nMatchMD5 %s\n", arg->asString());
    String md5 = arg->asString();
    // FlashFile md("lastbuildPLC.md5", SPIFFS_O_RDONLY);
    // char buf[32];
    // md.read(buf, 32);
    // arg->reset();
    // if (md5 == String(buf))
    // {
    //     arg->add(1);
    // }
    // else
    //     arg->add(0);
    return arg;
}
wamp_param *AppendChunkToBlob(wamp_param *arg)
{
    core_debug("AppendChunkToBlob %s\n", arg->asString());
    String md5 = arg->asString();
    // FlashFile md("lastbuildPLC.md5", SPIFFS_O_RDONLY);
    // char buf[32];
    // md.read(buf, 32);
    // arg->reset();
    // if (md5 == String(buf))
    // {
    //     arg->add(1);
    // }
    // else
    //     arg->add(0);
    return arg;
}


int get_ws_status()
{
    return ws.is_connected();
}

void onJoin()
{
    ws.rpc_register("AppendChunkToBlob", AppendChunkToBlob);
    ws.rpc_register("StartPLC", StartPLC);
    ws.rpc_register("PurgeBlobs", PurgeBlobs);
    ws.rpc_register("SeedBlob", SeedBlob);
    ws.rpc_register("NewPLC", NewPLC);
    ws.rpc_register("SetTraceVariablesList", SetTraceVariablesList);
    ws.rpc_register("GetTraceVariables", GetTraceVariables);
    ws.rpc_register("StopPLC", StopPLC);
    ws.rpc_register("MatchMD5", MatchMD5);
    ws.rpc_register("GetPLCstatus", GetPLCstatus);
}
int wamp::begin(const u8 *ip, const u16 port, const u8 *realm)
{
    ws.on_join(onJoin);
    ws.begin(client1, (const char *)ip, port, (char *)realm, "/ws", "wamp.2.json");
    while (!ws.is_connected())
    {
        ws.loop();
        core_debug(".\n");
        delay(500);
    }
    core_debug("*");
}
#endif